package com.wxg.gui.table;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;

import com.wxg.domain.table.TableDomain;

public class JTableExample {
	
	private JFrame frame = null;
	
//	private JPanel rootpanel = null;
	
	private JTable table = null;
	
	private TableDomain td = null;

	public JTableExample(){
		td = new TableDomain();
		String frametitle = "Hello,JTable";
		frame = new JFrame(frametitle);
	}
	
	
	private void addTableToFrame(){
		table = new JTable(new MyTableModel());
		frame.add(new JScrollPane(table), BorderLayout.CENTER);
	}
	
	public void init(){
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				addTableToFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setSize(400, 300);
				frame.setVisible(true);
			}
		});
	}

	public static void main(String[] args) {
		JTableExample jte = new JTableExample();
		jte.init();
	}
	
	/**
	 * 内部类：  TableModel
	 * @author wxg
	 *
	 */
	class MyTableModel extends AbstractTableModel{

		private static final long serialVersionUID = -4513223193721416070L;

		public MyTableModel(){
			td.head("Name", "Age", "Msg");
			td.data("wxg", 32, "你好");
			td.data("Jack", 23, "Hello");
		}
		
		@Override
		public String getColumnName(int column) {
			return td.head()[column];
		}
		
		@Override
		public int getRowCount() {
			return td.data().length;
		}

		@Override
		public int getColumnCount() {
			return td.head().length;
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			return td.data()[rowIndex][columnIndex];
		}
		
	}
	
}
